package com.applink.sell_proof;

import java.util.Observable;
import java.util.Observer;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager.BadTokenException;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.applink.sell_proof.api.DataHelper;
import com.applink.sell_proof.api.DataResult;
import com.applink.sell_proof.api.Methods;

public class ForgotPassword extends Activity implements OnClickListener,Observer {
	
	
	public static final String TAG = "ForgotPassword";
	private Button btnResetPass;
	private TextView txtEmailAddress;
	private Dialog uDialog = null;
	private Context mContext;
	private Dialog mProgress;
	private String responseCode = "";
	private boolean checkForgotPassword = false;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.forgot_password);
		initView();
	}
	
	private void initView(){
		mContext = this;
		if(btnResetPass == null){
			btnResetPass = (Button) findViewById(R.id.btn_reset_pass);
			btnResetPass.setOnClickListener(this);
		}
		
		if(txtEmailAddress == null){
			txtEmailAddress = (TextView) findViewById(R.id.email_address);
		}
	}
	
//	@Override
//	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//		View view = inflater.inflate(R.layout.forgot_password, null, false);
//		view.findViewById(R.id.btn_reset_pass).setOnClickListener(this);
//		return view;
//	}
//	
	@Override
	public void onResume() {
		super.onResume();
		DataHelper.getInstance().addObserver(this);
	}
	
	@Override
	public void onPause() {
		super.onPause();
		DataHelper.getInstance().deleteObserver(this);
	}

	@Override
	public void onClick(View v) {
		String email = txtEmailAddress.getText().toString().trim();
		switch (v.getId()) {
		case R.id.btn_reset_pass:
			if(email.trim().length() == 0) {
				Toast.makeText(mContext, "Please enter your emal address", Toast.LENGTH_SHORT).show();
				//showAlertDialog(null, "Forget password", "Close", null, "enter your email address", true);
				return;
			}
			DataHelper.getInstance().resetPassword(email);
			showProgressDialog();
			break;

		default:
			break;
		}		
	}

	@Override
	public void update(Observable observable, Object data) {
		dismissProgressDialog();
		if(data == null) return;
		DataResult result = (DataResult) data;
		if(result.getMethodName() == Methods.resetPass){
			//toggleLoading(false);
			if(result.getErrorCode() == null){
				responseCode = (String) result.getData();
//				if(responseCode == 0) {
//					showAlertDialog(mContext,android.R.drawable.ic_dialog_alert,"Error","Forget Password");
//				} else 
				if(responseCode.equals("True")) {
					checkForgotPassword = true;
					showAlertDialog(mContext,android.R.drawable.ic_dialog_alert,"Your password has been sent to your inbox","Forget Password");
					//showAlertDialog(null, "Forget password", "Close", null, "Open your email to get new password!", true);
//					EMarketing.getInstance().setLogin(false);
//					DialogFragment login = new LogIn();
//					login.show(getActivity().getSupportFragmentManager(), LogIn.TAG);
//					dismiss();
				} 
			} else {
				showAlertDialog(mContext,android.R.drawable.ic_dialog_alert,"Your e-mail is not registered in our program","Forget Password");
				//showAlertDialog(null, "Forgot password", "Close", null, "Error", true);
			}
		}
	}
	
	private void showAlertDialog(final Context mContext, int icon,
			String message, String title) {
		LayoutInflater inflater = (LayoutInflater) mContext
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View layout = inflater.inflate(R.layout.textdialog, null);
		TextView textView = (TextView) layout.findViewById(R.id.txtDialog);
		textView.setText(message);
		AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
		builder.setTitle(title);
		builder.setView(layout);
		builder.setCancelable(false);
		builder.setPositiveButton(android.R.string.ok,
				new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						if(checkForgotPassword){
							checkForgotPassword = false;
							finish();
						}
					}
				});

		builder.setIcon(icon);
		try {
			uDialog = builder.create();
			uDialog.show();
		} catch (BadTokenException e) {
		}
	}
	
	
//	private void toggleLoading(boolean show){
//		DialogFragment loading = (DialogFragment) getActivity().getSupportFragmentManager().findFragmentByTag(WaitingDialog.TAG);
//		if(show) {
//			if(loading != null) loading.show(getFragmentManager(), WaitingDialog.TAG);
//			else {
//				loading = new WaitingDialog(android.R.color.transparent);
//				loading.show(getFragmentManager(), WaitingDialog.TAG);
//			}
//		} else if(loading != null) {
//			loading.dismiss();
//		}
//	}
//	
//	private void showAlertDialog(DialogInterface.OnClickListener listener, String title, String navigative, String positive, String message, boolean cancelable) {
//		WaitingDialog waitingDialog = (WaitingDialog) getActivity().getSupportFragmentManager().findFragmentByTag(WaitingDialog.TAG);
//		if(waitingDialog != null && waitingDialog.isFullScreen()) {
//			waitingDialog.dismiss();
//		}
//		DialogFragment dialogFragment = new EAlertDialog(listener,title, navigative, message);
//		dialogFragment.setCancelable(cancelable);
//		dialogFragment.show(getActivity().getSupportFragmentManager(), null);
//	}
	
	
	void showProgressDialog() {
		runOnUiThread(new Runnable() {
			@Override
			public void run() {
				if (mProgress == null) {
					mProgress = new Dialog(ForgotPassword.this,android.R.style.Theme_Translucent_NoTitleBar);
					//mProgress = new Dialog(mContext,android.R.style.Theme_Translucent_NoTitleBar);							
					mProgress.setContentView(R.layout.progressdialog);
					mProgress.setCancelable(false);
					mProgress.show();
				} else {
					mProgress.show();
				}
			}
		});
	}

	void dismissProgressDialog() {
		if (mProgress != null && mProgress.isShowing()) {
			runOnUiThread(new Runnable() {
				@Override
				public void run() {
					mProgress.dismiss();
				}
			});
		}
	}
	
	
	

}
